package com.bbcloud.controller.portal;

import com.bbcloud.common.ServerResponse;
import com.bbcloud.pojo.Day;
import com.bbcloud.service.IDayService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by liuzhu on 2018/11/23.
 */
@Controller
@RequestMapping("/day/")
public class DayController {

    @Autowired IDayService iDayService;

    /**
     * 获取某一天的所有活动list
     * @param token     token
     * @param userId    userID
     * @param dayId     dayID
     * @param pageNum   第几页
     * @param pageSize  每页大小
     * @param orderBy   排序
     * @return  活动list
     */
    @RequestMapping("activity_list.do")
    @ResponseBody
    public ServerResponse<PageInfo> list(@RequestParam(value = "token", required = false) String token, @RequestParam(value = "userId", required = false) String userId, @RequestParam(value = "dayId", required = false) String dayId, @RequestParam(value = "pageNum", defaultValue = "1") int pageNum, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize, @RequestParam(value = "orderBy", defaultValue = "") java.lang.String orderBy) {

        return iDayService.getActivityByDayId(token, userId, dayId, pageNum, pageSize, orderBy);
    }

    /**
     *
     * @param token token
     * @param userId    userId
     * @param dayId dayId
     * @param activityLines 经纬度集合
     * @param deviceId  deviceId
     * @param distance  距离
     * @param usedTime  耗时
     * @param speedAvg  平均速度
     * @return  void
     */
    @RequestMapping("save_activity.do")
    @ResponseBody
    public ServerResponse saveActivity(@RequestParam(value = "token", required = false) String token, @RequestParam(value = "userId", required = false) String userId, @RequestParam(value = "dayId", required = false) String dayId,@RequestParam(value = "activityLines", required = false) String activityLines,@RequestParam(value = "deviceId", required = false) String deviceId,@RequestParam(value = "distance", defaultValue = "0") int distance,@RequestParam(value = "usedTime", defaultValue = "0") int usedTime,@RequestParam(value = "speedAvg", defaultValue = "0") int speedAvg){
        return iDayService.saveActivity(token,userId,dayId,activityLines,deviceId,distance,usedTime,speedAvg);
    }

    /**
     *
     * @param token token
     * @param userId    userId
     * @param dayId dayId
     * @param description   描述
     * @param file  图片
     * @return  Day
     */
    @RequestMapping("clock.do")
    @ResponseBody
    public ServerResponse<Day> clock(@RequestParam(value = "token", required = false) String token, @RequestParam(value = "userId", required = false) String userId, @RequestParam(value = "dayId", required = false) String dayId, @RequestParam(value = "description", required = false) String description, @RequestParam(value = "upload_file",required = false) MultipartFile file,HttpServletRequest request){
        String path = request.getSession().getServletContext().getRealPath("upload");
        return iDayService.clock(token,userId,dayId,description,path,file);
    }

    @RequestMapping("select_day.do")
    @ResponseBody
    public ServerResponse<Day> selectDayWithDayIdAndUserId(@RequestParam(value = "token", required = false) String token, @RequestParam(value = "userId", required = false) String userId, @RequestParam(value = "dayId", required = false) String dayId){
        return iDayService.selectDayWithDayIdAndUserId(token,userId,dayId);
    }
}
